package cn.com.xinjinfu.pdoulcProduct.manager;

import android.content.Context;

import com.alibaba.fastjson.JSON;

import org.json.JSONObject;

import cn.com.xinjinfu.pdoulcProduct.BaseApplication;
import cn.com.xinjinfu.pdoulcProduct.entity.User;
import cn.com.xinjinfu.pdoulcProduct.utils.SharedPreferencesUtils;

/**
 * 保存用户信息专用类
 * Created by Administrator on 2016/1/17 0017.
 */
public class AccountManager {

    private static AccountManager mInstance;
    private Context context;
    private User user = null;
    private AccountManager() {
        context = BaseApplication.getInstance().getApplicationContext();
    }

    public synchronized static AccountManager getInstance() {
        if (null == mInstance) {
            mInstance = new AccountManager();
        }
        return mInstance;
    }
    private static final String SAVE_USER_FILENAME = "PidouUser";


    /**
     * 保存用户
     *
     * @param jobj
     */
    public void saveUserInfoToFile(final JSONObject jobj) {
        if (jobj!=null){
            SharedPreferencesUtils.saveUserInfo(context, SAVE_USER_FILENAME, jobj.toString());
        }

    }

    public synchronized User getUser() {
        if (SharedPreferencesUtils.getUserInfo(context, SAVE_USER_FILENAME)!="") {
            user = JSON.parseObject(SharedPreferencesUtils.getUserInfo(context, SAVE_USER_FILENAME), User.class);
            return user;
        }
        return null;
    }

}
